Write a custom CUDA kernel to replace PyTorch's Focal Loss with Sigmoid implementation for binary classification.

You are given the following PyTorch architecture:

python
import torch
import torch.nn as nn
import torch.nn.functional as F

class Model(nn.Module):
"""
Focal Loss with Sigmoid implementation for binary classification.
Fully fused version that computes sigmoid and focal loss in a single pass.
Focal Loss = -α * (1-pt)^γ * log(pt)
where pt = p if target=1, else (1-p), p = sigmoid(logit)
"""
def init(self, alpha=0.25, gamma=2.0, reduction='mean'):
super(Model, self).init()
self.alpha = alpha
self.gamma = gamma
self.reduction = reduction

def forward(self, inputs: torch.Tensor, targets: torch.Tensor) -> torch.Tensor:
    """
    Compute Focal Loss with fused sigmoid computation.
    
    Args:
        inputs (torch.Tensor): Predicted logits of shape (batch_size, num_classes)
        targets (torch.Tensor): Ground truth labels of shape (batch_size,)
    
    Returns:
        torch.Tensor: Computed focal loss
    """
    # Ensure input types are consistent
    inputs = inputs.to(torch.float32)
    targets = targets.to(torch.float32)
    
    # Handle shape matching
    if inputs.dim() == 2 and inputs.size(1) == 1:
        inputs = inputs.squeeze(1)
    
    # Fully fused computation using highly optimized built-in functions
    # Compute sigmoid with numerical stability
    sigmoid_inputs = torch.sigmoid(inputs)
    
    # Compute pt based on targets
    pt = torch.where(targets == 1, sigmoid_inputs, 1 - sigmoid_inputs)
    
    # Compute focal weight
    focal_weight = self.alpha * torch.pow(1 - pt, self.gamma)
    
    # Compute binary cross entropy with logits (more numerically stable)
    bce = F.binary_cross_entropy_with_logits(inputs, targets, reduction='none')
    
    # Apply focal weight
    focal_loss = focal_weight * bce
    
    # Apply reduction
    if self.reduction == 'mean':
        return focal_loss.mean()
    elif self.reduction == 'sum':
        return focal_loss.sum()
    else:
        return focal_loss
batch_size = 32
num_classes = 1

def get_inputs():
# Generate random logits with explicit float32
inputs = torch.randn(batch_size, num_classes, dtype=torch.float32)
# Generate random binary targets (0 or 1) with explicit float32
targets = torch.randint(0, 2, (batch_size,), dtype=torch.float32)
return [inputs, targets]

def get_init_inputs():
return []



Your task is to optimize this Focal Loss with Sigmoid implementation by:

1. **Complete Operator Fusion**: Combine the sigmoid computation and focal loss calculation into a single CUDA kernel to eliminate intermediate tensor storage and redundant computations. The kernel should compute sigmoid, pt, focal weight, and BCE loss in one fused operation.

2. **Enhanced Numerical Stability**: Implement numerically stable sigmoid computation with conditional branches for positive/negative logits, use more aggressive epsilon bounds (1e-8), and directly compute BCE without intermediate steps to avoid precision loss.

3. **Memory Access Optimization**: Minimize global memory access by keeping all intermediate computations (sigmoid, pt, focal_weight, bce) in registers, and ensure coalesced memory access patterns for both logits and targets.

4. **Optimized Thread Configuration**: Use optimal block size of 256 threads with dynamic grid computation based on batch size, and implement efficient kernel launch parameters.

5. **Type and Shape Consistency**: Ensure all tensors use float32 for consistency, handle shape matching automatically (squeeze dim=1 when needed), and maintain proper device placement.

The optimized CUDA kernel should:
- Take logits and targets as input (both float32)
- Compute sigmoid, pt, focal weight, and BCE loss in a single fused kernel
- Use optimized sigmoid computation with numerical stability
- Apply more aggressive epsilon clamping (1e-8) for better stability
- Directly compute BCE without intermediate probability storage
- Output the fused focal loss values
- Support both 'mean' and 'sum' reduction modes
- Use optimized compilation flags (-O3, --use_fast_math)
- Achieve significant speedup (1.4-1.6x) over the original PyTorch implementation through fused computation and reduced memory overhead

Follow the inline CUDA extension syntax example provided in the reference. The kernel should demonstrate performance improvements through complete operator fusion, enhanced numerical stability, and optimized memory access patterns.
